package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResult<User> list(UserDto userdto) {
        PageHelper.startPage(userdto.getPage(), userdto.getPageSize());
        List<User> userList = userMapper.selectlist(userdto);
        Page<User> user = (Page<User>)userList;
        return new PageResult<>(user.getTotal(),user.getResult());
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        //user.setPassword(user.getUsername() + "123");
        user.setPassword(DigestUtils.md5DigestAsHex((user.getUsername()+"123").getBytes()));
        userMapper.update(user);
    }

    @Override
    public void deleteById(List<Integer> ids) {
        userMapper.deleteById(ids);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public void add(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        //user.setPassword(user.getUsername() + "123");
        user.setPassword(DigestUtils.md5DigestAsHex((user.getUsername()+"123").getBytes()));
        userMapper.add(user);
    }

    @Override
    public List<User> all() {
        return userMapper.all();
    }

    @Override
    public List<User> getByRole(String roleLabel) {
        return userMapper.getByRole(roleLabel);
    }

    @Override
    public List<User> getByDept(Integer deptId) {
        return userMapper.getByDept(deptId);
    }

    @Override
    public LoginResultVo login(User user) {
        User user1 = userMapper.selectByRole(user.getUsername());
        if(user1 == null){  //用户名不存在
            throw new BusinessException("用户名或密码错误");
        }
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if(!password.equals(user1.getPassword())){   //密码不正确
            throw new BusinessException("用户名或密码错误");
        }
        if(user1.getStatus() != 1){  //用户状态不正确
            throw new BusinessException("用户状态异常");
        }
        //给返回的vo赋值
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(user1.getId());
        loginResultVo.setName(user1.getName());
        loginResultVo.setUsername(user1.getUsername());
        loginResultVo.setImage(user1.getImage());
        loginResultVo.setRoleLabel(user1.getRoleLabel());

        Map<String, Object> claims = new HashMap<>();
        claims.put("id",user1.getId());
        claims.put("username",user1.getUsername());
        String jwt = JwtUtils.generateToken(claims);

        loginResultVo.setToken(jwt);
        return loginResultVo;
    }
}
